package com.course.domain.service.impl;

import com.course.domain.dao.AccountDao;
import com.course.domain.dao.StudentDao;
import com.course.domain.dao.TeacherDao;
import com.course.domain.enums.Role;
import com.course.domain.model.Account;
import com.course.domain.model.Student;
import com.course.domain.model.Teacher;
import com.course.domain.service.UserService;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by Justin on 2017/5/14.
 */
@Service
public class UserServiceImpl implements UserService {

    private final AccountDao accountDao;
    private final StudentDao studentDao;
    private final TeacherDao teacherDao;

    @Autowired
    public UserServiceImpl(AccountDao accountDao,
                           StudentDao studentDao,
                           TeacherDao teacherDao) {
        this.accountDao = accountDao;
        this.studentDao = studentDao;
        this.teacherDao = teacherDao;
    }

    @Transactional
    @Override
    public Student createStudent(String username, String password, Student student) {
        Preconditions.checkNotNull(username);
        Preconditions.checkNotNull(password);
        Preconditions.checkNotNull(student);
        Account account = saveAccount(username, password, Role.STUDENT);
        student.setAccountId(account.getId());
        student.setCourseId(0);
        student.setAgendaId(0);
        student.setStatus(1);
        return studentDao.save(student);
    }

    @Transactional
    @Override
    public Teacher createTeacher(String username, String password, Teacher teacher) {
        Preconditions.checkNotNull(username);
        Preconditions.checkNotNull(password);
        Preconditions.checkNotNull(teacher);
        Account account = saveAccount(username, password, Role.TEACHER);
        teacher.setAccountId(account.getId());
        teacher.setStatus(1);
        return teacherDao.save(teacher);
    }

    @Override
    public Account updateAccount(Account account) {
        Preconditions.checkNotNull(account);
        return accountDao.save(account);
    }

    @Override
    public Student updateStudent(Student student) {
        Preconditions.checkNotNull(student);
        return studentDao.save(student);
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        Preconditions.checkNotNull(teacher);
        return teacherDao.save(teacher);
    }

    @Override
    public Student deleteStudent(Student student) {
        student.setStatus(0);
        return studentDao.save(student);
    }

    @Override
    public Teacher deleteTeacher(Teacher teacher) {
        teacher.setStatus(0);
        return teacherDao.save(teacher);
    }

    @Override
    public Account loadAccount(int accountId) {
        return accountDao.findOne(accountId);
    }

    @Override
    public Student loadStudent(int studentId) {
        return studentDao.findOne(studentId);
    }

    @Override
    public Teacher loadTeacher(int teacherId) {
        return teacherDao.findOne(teacherId);
    }

    @Override
    public Account loadAccountByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return accountDao.findByUsername(username);
    }

    @Override
    public Student loadStudentByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        Account account = accountDao.findByUsername(username);
        if (account == null) {
            return null;
        }
        return studentDao.findByAccountId(account.getId());
    }

    @Override
    public Teacher loadTeacherByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        Account account = accountDao.findByUsername(username);
        if (account == null) {
            return null;
        }
        return teacherDao.findByAccountId(account.getId());
    }

    @Override
    public Student loadStudentByAccount(int accountId) {
        return studentDao.findByAccountId(accountId);
    }

    @Override
    public Teacher loadTeacherByAccount(int accountId) {
        return teacherDao.findByAccountId(accountId);
    }

    @Override
    public List<Account> listAccount() {
        return accountDao.findAll();
    }

    @Override
    public List<Student> listStudent() {
        return studentDao.findAllByStatus(1);
    }

    @Override
    public List<Teacher> listTeacher() {
        return teacherDao.findAllByStatus(1);
    }

    @Override
    public List<Account> findAccount(List<Integer> accountIds) {
        return accountDao.findAll(accountIds);
    }

    @Override
    public List<Student> findStudent(List<Integer> studentIds) {
        return studentDao.findAll(studentIds);
    }

    @Override
    public List<Teacher> findTeacher(List<Integer> teacherIds) {
        return teacherDao.findAll(teacherIds);
    }

    @Override
    public Page<Account> pageAccount(Pageable pageable) {
        Preconditions.checkNotNull(pageable);
        return accountDao.findAll(pageable);
    }

    @Override
    public Page<Student> pageStudent(Pageable pageable) {
        Preconditions.checkNotNull(pageable);
        return studentDao.findAll(pageable);
    }

    @Override
    public Page<Teacher> pageTeacher(Pageable pageable) {
        Preconditions.checkNotNull(pageable);
        return teacherDao.findAll(pageable);
    }

    private Account saveAccount(String username, String password, Role role) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setRole(role);
        account.setOAuthWx("");
        account.setStatus(1);
        return accountDao.save(account);
    }
}
